package com.example.yunhailutong.zhongtan.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;


import com.example.yunhailutong.zhongtan.weight.LoadingDialog;
import com.zhy.autolayout.AutoLayoutActivity;


public abstract class ActivityBase extends AutoLayoutActivity implements
        OnClickListener {
    public static String TAG;
    public Context mContext;
    // 这个对象的高度不能用 因为包含了通知栏
    protected DisplayMetrics metrics;
    // 这个是应用展示的高度
    protected Rect outRect = new Rect();

    private static final boolean debug = true;
    private LoadingDialog mWaitingDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置在4.4以上透明通知栏
//        Window window = getWindow();
//        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
//                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        mContext = this;
        TAG = this.getClass().getSimpleName();
        setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //无title
      //  requestWindowFeature(Window.FEATURE_NO_TITLE);
   //       getSupportActionBar().hide();
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (contentViewById() != 0) {
            setContentView(contentViewById());
        }
        //沉浸式
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            setTranslucentStatus(true, this);
//        }
//        initMintManager(int color);
        if (isSettingManager())
            initActivity(savedInstanceState);
        addListener();

    }


    @TargetApi(19)
    public static void setTranslucentStatus(boolean on, Activity activity) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 绑定控件id
     */
    protected abstract void addListener();

    /**
     * 初始化
     */
    protected abstract void initActivity(Bundle savedInstanceState);

    /**
     * 初始化控
     */

    protected abstract int contentViewById();


    protected boolean isSettingManager() {
        return true;
    }

    /**
     * 通过类名启动Activity
     *
     * @param pClass
     */
    protected void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    public DisplayMetrics obtainMetrice() {
        WindowManager windowManager = this.getWindowManager();
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }

    /**
     * 通过类名启动Activity，并且含有Bundle数据
     *
     * @param pClass
     * @param pBundle
     */
    protected void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    /**
     * 通过Action启动Activity
     *
     * @param pAction
     */
    protected void openActivity(String pAction) {
        openActivity(pAction, null);
    }

    /**
     * 通过Action启动Activity，并且含有Bundle数据
     *
     * @param pAction
     * @param pBundle
     */
    protected void openActivity(String pAction, Bundle pBundle) {
        Intent intent = new Intent(pAction);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    protected void disPlay(String content) {
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }

    protected void log(String content) {
        if (content == null) return;
        if (debug)
            Log.i(TAG, content);
    }

    public static void log(String tag, String content) {
        if (debug) Log.i(tag, content);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 传入viewid 添加为点击事件
     *
     * @param viewId
     */
    public ActivityBase addClick(int viewId) {
        findViewById(viewId).setOnClickListener(this);
        return this;
    }

    /**
     * 传入viewid 添加为点击事件
     *
     * @param viewId
     */
    public ActivityBase addClickForBack(int viewId) {
        findViewById(viewId).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        return this;
    }

    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (metrics == null) {
            WindowManager wm = getWindowManager();
            metrics = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(metrics);
            this.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        }
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 暂时放在这
     */
    public int dip2px(float dpValue) {
        return dip2px(this, dpValue);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    protected void showWaitingDialog() {
        if (mWaitingDialog == null) {
            mWaitingDialog = LoadingDialog.createDialog(getActivity());
        }
        mWaitingDialog.show();
    }

    protected void dismissWaitingDialog() {
        if (mWaitingDialog != null) {
            mWaitingDialog.dismiss();
        }
    }


    public void onPause() {
        super.onPause();
    }

    public boolean isShow = true;

    protected void toggleSoftInput() {
        // 隐藏输入法
        InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
    }

}
